package com.comunicacion2;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class ScanIntensidadWifi extends BroadcastReceiver {
	
	private Handler h;
	private WifiManager wifi;
	private String red;
	
	public ScanIntensidadWifi(Handler h, WifiManager wifi, String red) {
		super();
		this.h = h;
		this.wifi = wifi;
		this.red = red;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
	    List<ScanResult> results = wifi.getScanResults();
	    
	    DatosRed datosRed = new DatosRed(0,0);
	    
	    WifiInfo wifiInfo = wifi.getConnectionInfo();
	    String s = wifiInfo.getSSID();
	    if((wifiInfo!=null) && (wifiInfo.getSSID().equals("\""+red+"\""))){
	    	datosRed.setIp(wifiInfo.getIpAddress());
	    }
	    
	    //int level=0;
	    
	    for(ScanResult result : results){
	    	if(result.SSID.equals(red)){
	    		datosRed.setLevel(result.level);
	    		//level = result.level;
	    		break;
	    	}
	    }
	    
		Message m=new Message();
		m.obj=datosRed;
		h.sendMessage(m);

	}

}
